﻿
using MesGLApp.DAL.RoleDB;
using MesGLApp.DAL.RoleDTO;
using MesGLApp.Tools;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesGLApp.BLL.Role
{
    public class DeptInfoControler
    {
        public List<BaseDeptDTO> SearchAllDeptIntoList()
        {
            List<BaseDeptDTO> resList = new List<BaseDeptDTO>();
            using (RoleDB db = new RoleDB())
            {
                var query = db.base_dept;
                if (query != null && query.Count() > 0)
                {
                    foreach (BaseDeptModel deptModel in query)
                    {
                        BaseDeptDTO deptDTO = new BaseDeptDTO();
                        Tools.EntityTools.CopyEntity<BaseDeptModel, BaseDeptDTO>(deptModel,ref deptDTO);
                        resList.Add(deptDTO);
                    }
                }
            }
            return resList;
        }


        public List<TreeNode> getDeptTreeNodes(int pid = 0)
        {
            var deptNode = new List<TreeNode>();
            using (RoleDB db = new RoleDB())
            {
                var depts = db.base_dept.Where(u => u.parent_id == pid)
                            .ToList();

                foreach (var dept in depts)
                {
                    TreeNode node = new TreeNode();
                    node.Name = dept.id.ToString();
                    node.Text = dept.dept_name.ToString();
                    node.Toggle();
                    node.Nodes.AddRange(getDeptTreeNodes(dept.id).ToArray());
                    deptNode.Add(node);
                }
            }

            return deptNode;
        }

        public List<BaseDeptModel> getDepts(int pid = 0,int level=0,List<BaseDeptModel> deptList=null) 
        {
            if (deptList == null) 
            { 
                deptList = new List<BaseDeptModel>();
            }

            using (RoleDB db = new RoleDB()) 
            {
                var depts = db.base_dept.Where(u => u.parent_id == pid && !u.is_del)
                           .ToList();

                string levels = "";

                for (int i=0;i<level;i++) 
                {
                    levels += "|-";
                }


                foreach (var dept in depts) 
                { 
                     dept.dept_name = levels+dept.dept_name;
                     deptList.Add(dept);
                     getDepts(dept.id, level + 1, deptList);
                }
            
            }

           return deptList;
        }


        public BaseDeptModel getDept(int id)
        {
            BaseDeptModel dept = new BaseDeptModel();

            using (RoleDB db = new RoleDB())
            {
                dept = db.base_dept.Where(u => u.id == id).FirstOrDefault();

            }
            return dept;
        }

        /// <summary>
        /// 新增或者修改部门
        /// </summary>
        /// <param name="baseDeptModel"></param>
        /// <returns></returns>
        public int addDept(BaseDeptModel baseDeptModel)
        {
            using (RoleDB db = new RoleDB())
            {
                if (baseDeptModel.id == 0)
                {
                    baseDeptModel.is_del = false;
                    baseDeptModel.dept_code = db.Database.SqlQuery<string>("getLsh @p", new SqlParameter("@p", "Dept")).First();
                    db.base_dept.Add(baseDeptModel);
                }
                else
                {
                    var dept = db.base_dept.Where(u => u.id == baseDeptModel.id).FirstOrDefault();
                    if (dept != null)
                    {
                        dept.dept_name = baseDeptModel.dept_name;
                        dept.parent_id = baseDeptModel.parent_id;
                        dept.comp_name = baseDeptModel.comp_name;
                    }
                }

                var res = db.SaveChanges();
                return res;
            }
        }


        public int delDept(int id)
        {
            using (RoleDB db = new RoleDB())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        var dept = db.base_dept.Where(u => u.id == id).FirstOrDefault();
                        db.base_dept.Remove(dept);
                        db.Database.SqlQuery<int>("declineLsh @p", new SqlParameter("@p", "Dept")).First();
                        var res = db.SaveChanges();

                        tran.Commit();
                        return res;
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        throw ex;
                    }
                }
            }

        }

    }
}
